<?php
namespace app\admin\controller;
use app\AdminBaseController;
use think\facade\Db;
use app\admin\model\FamousModel;
use app\admin\model\MemberModel;

class Famous extends AdminBaseController {

    public function index() {
        $where = array();
        $countWhere = array();
        if(isset($_GET['mid'])) {
            $where[] =['a.mid', '=', $_GET['mid']];
            $countWhere[] =['mid', '=', $_GET['mid']];
        }
        if(isset($_GET['title'])&&!empty($_GET['title'])) {
            $name = $_GET['title'];
            $where[] = ['a.title','like',"%$name%"];
            $countWhere[] = ['title','like',"%$name%"];
        }
        $page = isset($_GET['page'])?$_GET['page']:1;
        $pageSize = isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $count = FamousModel::where($countWhere)->count('id');
        $list = FamousModel::field('a.id,a.title,a.content,a.updatetime,a.mid,m.name as mName')->where($where)->alias('a')->join(['yhq_member'=>'m'],'a.mid=m.id')->order('a.updatetime desc')->page($page,$pageSize)->select();
        foreach($list as $value) {
            $value['updatetime'] = date('Y-m-d H:m:s',$value['updatetime']);
        }
        return array(
            'count'=>$count,
            'list'=>$list
        );
    }

    public function add() {
        $params = array(
            'title'      =>  $_POST['title'],
            'mid'        =>  $_POST['mid'],
            'content'    =>  $_POST['content'],
            'addtime'    =>  time(),
            'updatetime' =>  time()
        );
        $result = FamousModel::insert($params);
        $res;
        if($result) {
            $res = array('success' => true,'message'=>'新增成功！');
        } else {
            $res = array('success' => false,'message'=>'新增失败！');
        }
        return $res;
    }

    public function update() {
        $params = array(
            'title'      =>  $_POST['title'],
            'mid'        =>  $_POST['mid'],
            'content'    =>  $_POST['content'],
            'addtime'    =>  time(),
            'updatetime' =>  time()
        );
        $result = FamousModel::where('id',$_POST['id'])->update($params);
        $res;
        if($result) {
            $res = array('success' => true,'message'=>'编辑成功！');
        } else {
            $res = array('success' => false,'message'=>'编辑失败！');
        }
        return $res;
    }

    public function getDetail() {
        $id = $_GET['id'];
        $article = FamousModel::where('id',$id)->find();
        $member = MemberModel::field('a.*,g.name as gName')->where('a.id',$article['mid'])->alias('a')->join(['yhq_generation'=>'g'],'a.gid=g.sort')->find();
        return array(
            'article'=>$article,
            'member'=>$member
        );
    }

    public function del() {
        $id = $_GET['id'];
        $result = FamousModel::where('id',$id)->delete();
        $res;
        if($result) {
            $res = array('success' => true,'message'=>'删除成功！');
        } else {
            $res = array('success' => false,'message'=>'删除失败！');
        }
        return $res;
    }
}